package com.qqtech.qquan.mall.controller;

import java.sql.Timestamp;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.mall.model.MallSubjectProduct;
import com.qqtech.qquan.mall.model.MallSubjectProductQuery;
import com.qqtech.qquan.mall.model.MallSubjectProductVo;
import com.qqtech.qquan.mall.service.MallSubjectProductService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.sys.model.SysMenu;

@Controller
@RequestMapping(value = "/mall/subject/product")
public class MallSubjectProductController extends BaseAdminController {
	@Resource
	private MallSubjectProductService mallSubjectProductService;
    
	@Override
	protected BaseService<MallSubjectProduct> getBaseService() {
		return mallSubjectProductService;
	}
	
	@RequestMapping(value = "/list/{subjectId}", method = RequestMethod.GET)
	public String list(@PathVariable Integer subjectId, HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("subjectId", subjectId);
		return "/mall/subject/product-list";
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(MallSubjectProductQuery query, HttpServletRequest request,
			HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(),
				new PageOrder(Direction.DESC, "id"));
		Page<MallSubjectProductVo> page = mallSubjectProductService.queryPageList(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Model model) {
		return "/mall/subject/product-update";
	}
	
	/**
	 * 进入选择商品页面
	 * @return
	 */
	@RequestMapping(value="/select/{id}",method=RequestMethod.GET)
	public String select(@PathVariable String id) {
		setAttribute("subjectId", id);
		return "/mall/subject/product-select";
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Result doAdd(MallSubjectProductVo mallSubjectProductVo, HttpServletRequest request,
			HttpServletResponse response) {
		Result ret = new Result();
		if (mallSubjectProductVo == null) {
			ret.addErr("参数不能为空");
		}
		if (ret.checkForm()) {
			MallSubjectProduct msp = new MallSubjectProduct();
			msp.setSubjectId(mallSubjectProductVo.getSubjectId());
			msp.setProductId(mallSubjectProductVo.getProductId());
			MallSubjectProductVo temp = mallSubjectProductService.getOne(msp);
			if(temp != null){
				ret.addErr("商品已存在");
			}else{
				msp.setOidx(0);
				Timestamp now = TimeUtil.now();
				msp.setNewAt(now);
				mallSubjectProductService.insert(msp);
			}
		}
		return ret;
	}
	
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response) {
		MallSubjectProductVo mallSubjectProductVo = mallSubjectProductService.getById(id);
		request.setAttribute("mallSubjectProductVo", mallSubjectProductVo);
		return "/mall/subject/product-update";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Result doUpdate(MallSubjectProductVo mallSubjectProductVo) {
		Result ret = new Result();
		if (mallSubjectProductVo == null || mallSubjectProductVo.getId() == null) {
			ret.addErr("参数不能为空");
		} else {
			if (mallSubjectProductVo.getOidx() == null) {
				ret.addErr("请输入排序");
			}
		}
		if (ret.checkForm()) {
			MallSubjectProduct mallSubjectProduct = new MallSubjectProduct();
			mallSubjectProduct.setId(mallSubjectProductVo.getId());
			mallSubjectProduct.setOidx(mallSubjectProductVo.getOidx());
			mallSubjectProductService.updateById(mallSubjectProduct);
		}
		return ret;
	}
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	@ResponseBody
   	public Result doDelete(@PathVariable Integer id) {
		Result ret = new Result();
		if (id == null) {
			ret.addErr("参数不能为空");
		} else{
			mallSubjectProductService.deleteById(id);
		}
   		return ret;
   	}
}
